package io.github.topcatv.mall.model.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.topcatv.mall.common.ao.PermissionAo;
import io.github.topcatv.mall.common.ao.ReturnObject;
import io.github.topcatv.mall.model.sys.entity.Permission;
import io.github.topcatv.mall.model.sys.service.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyi
 * @since 2019-10-14
 */
@Api(tags = "PermissionApi")
@RestController
@RequestMapping("/sys/permission")
public class PermissionController {
    @Resource
    IPermissionService permissionService;

    @ApiOperation(value = "删除用户权限", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "permissionId", value = "通过登陆获取的用户权限ID", required = true, defaultValue = "")
    })
    @DeleteMapping("/{permissionId}")
    public ReturnObject remove(@PathVariable("permissionId") Integer permissionId) {
        return permissionService.removeById(permissionId) ? ReturnObject.success() : ReturnObject.error("删除失败");
    }

    @ApiOperation(value = "添加权限", notes = "")
    @PostMapping("")
    public ReturnObject save(@RequestBody PermissionAo permissionAo) {
        Permission entity = permissionAo.toEntity();
        return permissionService.save(entity) ? ReturnObject.success().put("id", entity.getId()) : ReturnObject.error("新增失败");
    }

    @ApiOperation(value = "查询菜单", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页号", required = true, defaultValue = ""),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, defaultValue = "")
    })
    @GetMapping("/menu")
    public ReturnObject queryMenu(long pageNo, long pageSize) {
        QueryWrapper<Permission> query = new QueryWrapper<>();
        query.lambda().like(Permission::getType, Permission.TYPE_MENU);
        IPage<Permission> page = permissionService.page(new Page<>(pageNo, pageSize), query);
        return ReturnObject.success().put("permissions", page);
    }

    @ApiOperation(value = "查询功能权限", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页号", required = true, defaultValue = ""),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true, defaultValue = "")
    })
    @GetMapping("/action")
    public ReturnObject queryAction(long pageNo, long pageSize) {
        QueryWrapper<Permission> query = new QueryWrapper<>();
        query.lambda().like(Permission::getType, Permission.TYPE_ACTION);
        IPage<Permission> page = permissionService.page(new Page<>(pageNo, pageSize), query);
        return ReturnObject.success().put("permissions", page);
    }

}
